<cc:page type="sitepage" style="standard" showSidebar="false" showHeader="false" />

<cc>
    {





    /*开函客户的阶段变为 	转换成功 ,  给项目经理发送通知， 商机的升迁 ， 开函数据的储存   客户的修改为电子保函客户 */
    //这是一个Site页面，可以通过URL直接访问 上面是测试环境
    //  ServiceResult sr = CCService.getUserInfo("61ee0c8c32fe6ea38850a3475fa15750fbc1bb1a20768b71", "test_hhtest@cloudcc.com", "111111");
    //              ServiceResult sr = null;
    ServiceResult sr = CCService.getUserInfo("f6c729c175119d3aa2942d814a63df8564dfe85e8959db2d","interface@cloudcc.com","111111");
    UserInfo uuserInfo = (UserInfo) sr.get("COMMON_USERINFO");
    String resultMessage = "SUCCESS";
    String resultMessageError = "ERROR";
    String resultCode = "0000";
    String ACCOUNTTYPE = "转换维护";
    String ELEOPPTYPE = "4-落地开函";
    String ELEOPPTYPE3 = "3-客情调研";
    String TURETYPE = "已完结";
    String ACCOUNTTTYPT = "待分配";
    String GHXZJD = "已失效";

    Date date = new Date();
    SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String now = sf2.format(date);

    String ACCOUNTRECORDTYPE = "202385551266A597FF1B";
    /*公海池用户*/
    String gzghc = "00520228CDB952EH7KW0";
    String msg = "";
    CCService cs = new CCService(uuserInfo);

    String dataInfo = request.getParameter("data") == null ? "" : request.getParameter("data") + "";
    String invokeId = request.getParameter("invokeId") == null ? "" : request.getParameter("invokeId") + "";
    /*插入接口调用记录*/
    CCObject interface_record = new CCObject("interface_record");
    interface_record.put("url", "orderopening.jsp");
    interface_record.put("record_ID", invokeId);
    interface_record.put("datas", dataInfo);
    try {
    JSONObject jsonObject = JSONObject.fromObject(dataInfo);
    String cusCertNo = jsonObject.get("cusCertNo") == null ? "" : jsonObject.get("cusCertNo") + "";
    String lgNo = jsonObject.get("lgNo") == null ? "" : jsonObject.get("lgNo") + "";
    String customerName = jsonObject.get("customerName") == null ? "" : jsonObject.get("customerName") + "";
    String guaranteeCreateDate = jsonObject.get("guaranteeCreateDate") == null ? "" : jsonObject.get("guaranteeCreateDate") + "";
    String platformCode = jsonObject.get("platformCode") == null ? "" : jsonObject.get("platformCode") + "";
    String actualAmount = jsonObject.get("actualAmount") == null ? "" : jsonObject.get("actualAmount") + "";
    String guaranteeAmount = jsonObject.get("guaranteeAmount") == null ? "" : jsonObject.get("guaranteeAmount") + "";
    String operatorName = jsonObject.get("operatorName") == null ? "" : jsonObject.get("operatorName") + "";
    String operatorPhone = jsonObject.get("operatorPhone") == null ? "" : jsonObject.get("operatorPhone") + "";
    String findAccountIdSql = "select id,ownerid from Account where invoiceTaxNo='" + cusCertNo + "' and is_deleted='0' ";
    List<CCObject> accountList = cs.cqlQuery("Account", findAccountIdSql);

    String accountId = "";
    String userId = "";
    String  fzrId ="";
    if (accountList.size() > 0) {
    accountId = accountList.get(0).get("id")==null?"":accountList.get(0).get("id")+"";
    userId = accountList.get(0).get("ownerid")==null?"":accountList.get(0).get("ownerid")+"";
    String findSqlpsmc = "select id from psmc where uncode ='" + platformCode + "' and is_deleted='0' ";
    List<CCObject> ccObjects = cs.cqlQuery("psmc", findSqlpsmc);
        String fk_psmc_id="";
        if (ccObjects.size()>0) {
        fk_psmc_id=ccObjects.get(0).get("id")==null?"":ccObjects.get(0).get("id")+"";
        String findAccount = "select id,fk_ccuser from manage_group where  fk_account ='" + accountId + "' and fk_psmc ='"+fk_psmc_id+"' and is_deleted='0' and ghzt!='" + GHXZJD + "'";

        List<CCObject> manageGroupInfo = cs.cqlQuery("manage_group", findAccount);
            if (manageGroupInfo.size() > 0) {
            for (int i = 0; i < manageGroupInfo.size(); i++) {
            String groupId = manageGroupInfo.get(i).get("id") == null ? "" : manageGroupInfo.get(i).get("id") + "";

            userId = manageGroupInfo.get(i).get("fk_ccuser")==null?"": manageGroupInfo.get(i).get("fk_ccuser")+"";
            }
            }
            }
            /*更改客户的阶段 加 记录类型*/
            String updateAccountTypeSql = "update Account set follow_status = '" + ACCOUNTTYPE + "',recordtype ='" + ACCOUNTRECORDTYPE + "',deal_status='已开函' where id ='" + accountId + "' ";
            cs.cqlQuery("Account", updateAccountTypeSql);

            /*商机的升迁*/
            //   String updateEleOppType = "update eleopp set jieduan = '" + ELEOPPTYPE + "',status1='" + TURETYPE + "',open_time='" + guaranteeCreateDate + "' " +
            //         "where khmc ='" + accountId + "' and jieduan = '" + ELEOPPTYPE3 + "' and is_deleted='0' " +
            //       " and fk_psmc='"+fk_psmc_id+"'";
            //                String updateEleOppType ="update eleopp set jieduan = '"+ELEOPPTYPE+"' where khmc ='"+accountId+"' and jieduan = '"+ELEOPPTYPE3+"' and is_deleted='0' ";

            String findEleopp = "select * from eleopp where  khmc ='" + accountId + "' and jieduan = '" + ELEOPPTYPE3 + "' and is_deleted='0'  and fk_psmc='"+fk_psmc_id+"' ";
            List<CCObject> eleopp = cs.cqlQuery("eleopp", findEleopp);
                if (eleopp.size() > 0) {
                String openTime = eleopp.get(0).get("open_time") == null ? "" : eleopp.get(0).get("open_time") + "";
                String eleId = eleopp.get(0).get("id") == null ? "" : eleopp.get(0).get("id") + "";
                if (openTime.equals("")){
                String updateEleOppType = "update eleopp set jieduan = '" + ELEOPPTYPE + "',status1='" + TURETYPE + "',open_time='" + guaranteeCreateDate + "',timeThreeEnd='"+now+"',timeFourStart='"+now+"' where id ='"+eleId+"' ";
                cs.cqlQuery("eleopp", updateEleOppType);
                }else {
                String updateEleOppType = "update eleopp set jieduan = '" + ELEOPPTYPE + "',status1='" + TURETYPE + "',timeThreeEnd='"+now+"',timeFourStart='"+now+"' where id ='"+eleId+"' ";
                cs.cqlQuery("eleopp", updateEleOppType);
                }
                }



                /*电子保函开函信息*/
                CCObject manageGroup = new CCObject("ele_order");
                manageGroup.put("fk_account", accountId);
                manageGroup.put("name", lgNo);
                manageGroup.put("fk_ccuser", userId);
                manageGroup.put("open_time", guaranteeCreateDate);
                manageGroup.put("get_money", actualAmount);
                manageGroup.put("g_amount", guaranteeAmount);
                manageGroup.put("ownerid", userId);
                manageGroup.put("jbr", operatorName);
                String findSql = "select id from psmc where uncode ='" + platformCode + "' and is_deleted='0' ";
                List<CCObject> psmc = cs.cqlQuery("psmc", findSql);
                    if (psmc.size() > 0) {
                    manageGroup.put("fk_psmc", psmc.get(0).get("id"));
                    }

                    manageGroup.put("operatorPhone", operatorPhone);
                    cs.insert(manageGroup);


                    /* 发送开函通知 插入通知表*/
                    CCObject object = new CCObject("notice");
                    object.put("companyName", customerName);
                    object.put("accountId", userId);
                    object.put("record_id", accountId);
                    cs.insert(object);


                    JSONObject returnDataInfo = new JSONObject();
                    returnDataInfo.put("invokeId", invokeId);
                    returnDataInfo.put("resultCode", resultCode);
                    returnDataInfo.put("resultMessage", resultMessage);
                    interface_record.put("requestDataLong", returnDataInfo);
                    cs.insert(interface_record);
                    msg = String.valueOf(returnDataInfo);

                    }else {

                    /*还需要查询公招信息和工商信息*/
                    JSONObject requestDataInfo = new JSONObject();
                    requestDataInfo.put("invokeId", invokeId);
                    JSONObject dataInfos = new JSONObject();
                    dataInfos.put("creditCode", cusCertNo);
                    requestDataInfo.put("data", dataInfo);


                    /*公招信息*/
                    try {
                    /*公招信息*/
                    com.cloudcc.core.fag.CCUST_191127DFDFD896.CompanyInfoCountService companyInfoCountService = new com.cloudcc.core.fag.CCUST_191127DFDFD896.CompanyInfoCountService(uuserInfo);
                    String s = companyInfoCountService.sendPost(String.valueOf(requestDataInfo));
                    JSONObject jsonObject1 = JSONObject.fromObject(s);
                    JSONObject jsonObjectInfo = jsonObject1.getJSONObject("data");
                    CCObject eleOpenInfo = new CCObject("ele_open_info ");
                    eleOpenInfo.put("first_con_num", jsonObjectInfo.get("registeredConstructorsFirst"));
                    eleOpenInfo.put("second_con_num", jsonObjectInfo.get("registeredConstructorsSecond"));
                    eleOpenInfo.put("gczzsl", jsonObjectInfo.get("engineeringQualifications"));
                    eleOpenInfo.put("main_area", jsonObjectInfo.get("mainBiddingArea"));
                    eleOpenInfo.put("main_type", jsonObjectInfo.get("projectCategoryMoreThenFifty"));
                    cs.insert(eleOpenInfo);
                    } catch (Exception e) {
                    }


                    /*公商信息*/
                    com.cloudcc.core.fag.CCUST_191127DFDFD896.CompanyBaseInfoService information = new com.cloudcc.core.fag.CCUST_191127DFDFD896.CompanyBaseInfoService(uuserInfo);
                    net.sf.json.JSONObject requsetInfo1 = new net.sf.json.JSONObject();
                    net.sf.json.JSONObject requsetInfo = new net.sf.json.JSONObject();

                    requsetInfo1.put("invokeId", invokeId);
                    requsetInfo.put("creditCode", cusCertNo);
                    requsetInfo1.put("data", requsetInfo);
                    String informationInfoList = information.sendPost(String.valueOf(requsetInfo1));
                    try {
                    JSONObject informationInfo = JSONObject.fromObject(informationInfoList).getJSONObject("data");
                    /*客户及工商信息*/
                    CCObject accountInfo = new CCObject("Account");
                    String companyName = informationInfo.getString("companyName") == null ? "" : informationInfo.getString("companyName") + "";
                    accountInfo.put("name", companyName);
                    accountInfo.put("invoiceTaxNo", cusCertNo);
                    String address = informationInfo.getString("regAddress") == null ? "" : informationInfo.getString("regAddress") + "";
                    String legaRepresentative = informationInfo.getString("legaRepresentative") == null ? "" : informationInfo.getString("legaRepresentative") + "";
                    accountInfo.put("registeredAddress", address);
                    accountInfo.put("full_name", legaRepresentative);
                    String establishmentDate = informationInfo.getString("establishmentDate") == null ? "" : informationInfo.getString("establishmentDate") + "";
                    accountInfo.put("establishDate", establishmentDate);
                    String province = informationInfo.getString("province") == null ? "" : informationInfo.getString("province") + "";
                    accountInfo.put("province", province);
                    String cityCodeHh = informationInfo.getString("cityCodeHh") == null ? "" : informationInfo.getString("cityCodeHh") + "";
                    accountInfo.put("fk_city_name", cityCodeHh);
                    String countyCodeHh = informationInfo.getString("countyCodeHh") == null ? "" : informationInfo.getString("countyCodeHh") + "";
                    accountInfo.put("fk_country_name", countyCodeHh);
                    String companyType = informationInfo.getString("companyType") == null ? "" : informationInfo.getString("companyType") + "";
                    accountInfo.put("qyxz", companyType);
                    accountInfo.put("recordtype", ACCOUNTRECORDTYPE);
                    accountInfo.put("follow_status", ACCOUNTTTYPT);
                    accountInfo.put("ownerid", gzghc);
                    accountInfo.put("deal_status", "已开函");
                    String registeredCapital = informationInfo.getString("registeredCapital") == null ? "" : informationInfo.getString("registeredCapital") + "";
                    accountInfo.put("registered_capital", registeredCapital);

                    /*插入客户*/
                    ServiceResult insert = cs.insert(accountInfo);
                    String accountIdInfo = insert.get("id") == null ? "" : insert.get("id") + "";
                    //                String update = "update Account set recordtype ='"+ACCOUNTRECORDTYPE+"' where id ='"+accountIdInfo+"' ";
                    //                cs.cqlQuery("Account",update);
                    /*电子保函开函信息*/
                    CCObject manageGroup = new CCObject("ele_order");
                    manageGroup.put("fk_account", accountIdInfo);
                    manageGroup.put("name", lgNo);
                    manageGroup.put("fk_ccuser", userId);
                    manageGroup.put("open_time", guaranteeCreateDate);
                    manageGroup.put("get_money", actualAmount);
                    manageGroup.put("g_amount", guaranteeAmount);
                    manageGroup.put("jbr", operatorName);
                    String findSql = "select id from psmc where uncode ='" + platformCode + "' and is_deleted='0' ";
                    List<CCObject> psmc = cs.cqlQuery("psmc", findSql);
                        if (psmc.size() > 0) {
                        manageGroup.put("fk_psmc", psmc.get(0).get("id"));
                        }
                        manageGroup.put("operatorPhone", operatorPhone);
                        cs.insert(manageGroup);
                        JSONObject returnDataInfo = new JSONObject();
                        returnDataInfo.put("invokeId", insert.get("id"));
                        returnDataInfo.put("resultCode", resultCode);
                        returnDataInfo.put("resultMessage", resultMessage);
                        msg = String.valueOf(returnDataInfo);
                        } catch (Exception e) {

                        String resultMessageInfo = JSONObject.fromObject(informationInfoList).getString("resultMessage");
                        JSONObject returnDataInfo = new JSONObject();
                        returnDataInfo.put("invokeId", invokeId);
                        returnDataInfo.put("resultCode", resultCode);
                        returnDataInfo.put("resultMessage", resultMessageInfo);
                        interface_record.put("requestDataLong", returnDataInfo);
                        cs.insert(interface_record);
                        msg = String.valueOf(returnDataInfo);
                        }

                        }
                        request.setAttribute("jsonmsg", msg);

                        } catch (Exception e) {
                        JSONObject returnDataInfo = new JSONObject();
                        returnDataInfo.put("invokeId", invokeId);
                        returnDataInfo.put("resultCode", resultCode);
                        returnDataInfo.put("resultMessage", resultMessageError+e);
                        interface_record.put("requestDataLong", returnDataInfo);
                        cs.insert(interface_record);
                        msg = String.valueOf(returnDataInfo);
                        request.setAttribute("jsonmsg", msg);
                        }





                        }
</cc>
<cc:forward page="/platform/activity/task/getNodes.jsp">
    <cc:param name="jsonmsg" value="" />
</cc:forward>
